package com.itboye.ihomebank.activity.key;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.InputType;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ConfigBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import java.util.Observable;
import java.util.Observer;

/**
 * 预约安装
 * 
 */
public class ActivityKeyYuYue extends BaseOtherActivity implements Observer {
	View v_statusbar;
	TextView add_shap_title_tv;
	ImageView close_icon;
	TextView key_text;

	RelativeLayout key_rename, key_rephone, key_redizhi;
	TextView key_name, key_phone, key_dizhi;
	TextView mykey_add, mykey_yuyue;
	
	String uid;

	KeyPresenter keyPresenter;
	UserPresenter userPresenter;

	private Dialog dialog;// 自定义Dialog
	private int bs = 0;
	EditText dialog_et;
	
	private String phone;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.key_rename:// 姓名
			bs = 1;
			dialog_et.setHint("请输入姓名");
			dialog.show();
			break;
		case R.id.key_rephone:// 电话
			bs = 2;
			dialog_et.setHint("请输入手机号码");
			dialog_et.setInputType(InputType.TYPE_CLASS_NUMBER);
			dialog.show();
			break;
		case R.id.key_redizhi:// 地址
			bs = 3;
			dialog_et.setHint("请输入地址");
			dialog.show();
			break;

		case R.id.mykey_add:// 提交预约
			
			String name=key_name.getText().toString();
			if(name.equals("")){
				ByAlert.alert("请输入姓名");
				return;
			}
			String mobile=key_phone.getText().toString();
			if(mobile.equals("")){
				ByAlert.alert("请输入电话号码");
				return;
			}
			String address=key_dizhi.getText().toString();
			if(address.equals("")){
				ByAlert.alert("请输入地址");
				return;
			}
			showProgressDialog("提交中,请稍后", true);
			keyPresenter.getYuYue(uid, name, mobile, address);

			break;
		case R.id.mykey_yuyue:// 联系客服
			if (phone != null) {
				Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
						+ phone));
				startActivity(intent);
			} else {
				ByAlert.alert("暂未获取到客服信息");
			}
			break;

		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_key_yuyue;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		setStatusBarHeight(v_statusbar);
		add_shap_title_tv.setText("预约安装");
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";

		keyPresenter = new KeyPresenter(this);
		keyPresenter.getSuoXinXi();
		read();
		showExtraDialog();
		
		userPresenter = new UserPresenter(this);
		userPresenter.getConfig();

	}

	/**
	 * 读取
	 */
	private void read() {
		key_phone.setText(SPUtils.get(this, null, SPContants.MOBILE, "") + "");
		String sex = "";
		if ((SPUtils.get(this, null, SPContants.SEX, "") + "").equals("0")) {// 保密
			int length = (SPUtils.get(this, null, SPContants.RealName, "") + "")
					.length();
			StringBuffer buffer = new StringBuffer();
			for (int i = 0; i < length - 1; i++) {
				sex += "*";
			}

		} else if ((SPUtils.get(this, null, SPContants.SEX, "") + "")
				.equals("1")) {//
			sex = "女士";
		} else if ((SPUtils.get(this, null, SPContants.SEX, "") + "")
				.equals("2")) {
			sex = "先生";
		}
		if ((SPUtils.get(this, null, SPContants.RealName, "") != "")) {
			key_name.setText((SPUtils.get(this, null, SPContants.RealName, "") + "")
					.charAt(0) + sex);
		}
	}

	/**
	 * 自定义Dialog
	 */
	private void showExtraDialog() {
		dialog = new Dialog(this, R.style.rel_dialog);
		View view = LayoutInflater.from(this).inflate(R.layout.user_dialog_two,
				null);
		dialog.setContentView(view);
		dialog_et = (EditText) view.findViewById(R.id.dialog_et);

		TextView dialog_ok = (TextView) view.findViewById(R.id.dialog_ok);
		TextView dialog_quxiao = (TextView) view
				.findViewById(R.id.dialog_quxiao);
		dialog.setCanceledOnTouchOutside(false);
		dialog_ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String content = dialog_et.getText().toString();
				if (bs == 1) {
					key_name.setText(content);
				}
				if (bs == 2) {
					key_phone.setText(content);
				}
				if (bs == 3) {
					key_dizhi.setText(content);
				}
				dialog.dismiss();
			}
		});
		dialog_quxiao.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
			}
		});
		WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		// 屏幕宽度
		int screenWidth = display.getWidth();
		// 屏幕高度
		int screenHeight = display.getHeight();
		Window window = dialog.getWindow();

		WindowManager.LayoutParams lp = window.getAttributes();
		window.setGravity(Gravity.CENTER);
		lp.width = screenWidth * 3 / 4;
		lp.height = LayoutParams.WRAP_CONTENT;
		window.setAttributes(lp);

	}

	@Override
	protected void onReadIntent(Bundle bundle) {

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == KeyPresenter.suoXinXi_success) {
				key_text.setText("      " + entity.getData().toString());
			}
			if (entity.getEventType() == KeyPresenter.suoXinXi_fail) {

			}
			if (entity.getEventType() == KeyPresenter.yuyue_success) {
				ByAlert.alert(entity.getData());
			}
			if (entity.getEventType() == KeyPresenter.yuyue_fail) {
				ByAlert.alert(entity.getData());
			}
			if (entity.getEventType() == UserPresenter.getConfig_success) {
				ConfigBean configBean = (ConfigBean) entity.getData();
				phone = configBean.getCustomerPhone().get(0).getValue();
			} else if (entity.getEventType() == UserPresenter.getConfig_fail) {

			}

		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
		}
	}

}
